package com.yc.class_attendance.utils;

import android.content.Context;

import com.yc.class_attendance.entity.Attachment;
import com.yc.class_attendance.entity.MeetingCheck;
import com.yc.class_attendance.entity.MeetingResutl;
import com.yc.class_attendance.entity.PersonnelResutl;
import com.yc.class_attendance.entity.TempBean;

import java.util.List;

import io.realm.Realm;
import io.realm.RealmObject;
import io.realm.RealmResults;
import io.realm.Sort;

/**
 * 这个叫限制泛型的范围
 * Created by matou0289 on 2016/10/20.
 */

public class RealmHelper<E extends RealmObject> {
    public static final String DB_NAME = "myDogRealm.db";
    private Realm mRealm;


    public RealmHelper(Context context) {

        mRealm = Realm.getDefaultInstance();
    }

    /**
     * add （增）
     */
    public void addDog(final E dog) {
        mRealm.beginTransaction();
        mRealm.copyToRealm(dog);
        mRealm.commitTransaction();

    }

    /**
     * delete （删）
     */
    public void deleteDog(String id,String courseId ) {

        RealmResults<PersonnelResutl> dog = mRealm.where(PersonnelResutl.class).equalTo("courseId", id).equalTo("classId",courseId).findAll();
        mRealm.beginTransaction();
        dog.deleteAllFromRealm();
        mRealm.commitTransaction();
    }
    /**
     * delete （删）
     */
    public void deleteMeetingResutl(String id,String courseId) {

        RealmResults<MeetingResutl> dog = mRealm.where(MeetingResutl.class).equalTo("courseId", id).equalTo("courseId",courseId).findAll();
        mRealm.beginTransaction();
        dog.deleteAllFromRealm();
        mRealm.commitTransaction();
    }

    /**
     * delete （删）
     */
    public void deleteDogMeeting(String id,String courseId) {
        RealmResults<MeetingCheck> dog = mRealm.where(MeetingCheck.class).equalTo("courseId", id).equalTo("classId",courseId).findAll();
        mRealm.beginTransaction();
        dog.deleteAllFromRealm();
        mRealm.commitTransaction();
    }

    /**
     * 删除全部
     */
    public void deleteDogAll(Class<E> c) {
        RealmResults<E> dogs = mRealm.where(c).findAll();
        mRealm.beginTransaction();
        dogs.deleteAllFromRealm();
        mRealm.commitTransaction();
    }


    /**
     * update （改） 最后一条数据
     */
    public void UpdateLgLate(String wlkh) {
        MeetingCheck dog;
        RealmResults<MeetingCheck> realmResults = mRealm.where(MeetingCheck.class).equalTo("wlkh", wlkh).findAll();
        mRealm.beginTransaction();
        dog = realmResults.get(realmResults.size() - 1);
        dog.setLg("1");
        mRealm.commitTransaction();
    }

    /**
     * update （改） 最后一条数据
     */
    public void UpdateLgOne(String wlkh,String lg) {
        MeetingCheck dog;
        RealmResults<MeetingCheck> realmResults = mRealm.where(MeetingCheck.class).equalTo("wlkh", wlkh).findAll();
        mRealm.beginTransaction();
        dog = realmResults.get(0);
        dog.setLg(lg);
        mRealm.commitTransaction();
    }
    /**
     * update （改）第一条
     */
    public void updateAttendResultOne(String wlkh, String UserId, String InResult) {
        RealmResults<MeetingCheck> realmResults = mRealm.where(MeetingCheck.class).equalTo("wlkh", wlkh).equalTo("UserId",UserId).findAll();
        mRealm.beginTransaction();
        MeetingCheck dog = realmResults.get(0);
        dog.setAttendResult(InResult);
        mRealm.commitTransaction();
    }
    /**
     * update （改）第一条
     */
    public void updateAttendcourseId(String wlkh, String InResult) {
        RealmResults<MeetingCheck> realmResults = mRealm.where(MeetingCheck.class).equalTo("wlkh", wlkh).findAll();
        mRealm.beginTransaction();
        for (int i=0;i<realmResults.size();i++){
            realmResults.get(i).setCourseId(InResult);
        }
        mRealm.commitTransaction();
    }

    /**
     * update （改）第一条
     */
    public void updateAttendResult(String wlkh, String InResult) {
        RealmResults<MeetingCheck> realmResults = mRealm.where(MeetingCheck.class).equalTo("wlkh", wlkh).findAll();
        mRealm.beginTransaction();
        MeetingCheck dog = realmResults.get(realmResults.size() - 1);
        dog.setAttendResult(InResult);
        mRealm.commitTransaction();
    }

    /**
     * update （改）第一条
     */
    public void updateOutResultOne(String wlkh, String InResult) {
        RealmResults<MeetingCheck> realmResults = mRealm.where(MeetingCheck.class).equalTo("wlkh", wlkh).findAll();
        mRealm.beginTransaction();
        MeetingCheck dog = realmResults.get(0);
        dog.setOutResult(InResult);
        mRealm.commitTransaction();
    }

    /**
     * update （改）第一条
     */
    public void updateOutResult(String wlkh, String InResult) {
        RealmResults<MeetingCheck> realmResults = mRealm.where(MeetingCheck.class).equalTo("wlkh", wlkh).findAll();
        mRealm.beginTransaction();
        MeetingCheck dog = realmResults.get(realmResults.size() - 1);
        dog.setOutResult(InResult);
        mRealm.commitTransaction();
    }

    /**
     * update （改） 最后一条数据
     */
    public void updateInResultOne(String wlkh, String InResult) {
        RealmResults<MeetingCheck> realmResults = mRealm.where(MeetingCheck.class).equalTo("wlkh", wlkh).findAll();
        mRealm.beginTransaction();
        MeetingCheck dog = realmResults.get(0);
        dog.setInResult(InResult);
        mRealm.commitTransaction();
    }

    /**
     * update （改） 最后一条数据
     */
    public void updateInResult(String wlkh, String InResult) {
        RealmResults<MeetingCheck> realmResults = mRealm.where(MeetingCheck.class).equalTo("wlkh", wlkh).findAll();
        mRealm.beginTransaction();
        MeetingCheck dog = realmResults.get(realmResults.size() - 1);
        dog.setInResult(InResult);
        mRealm.commitTransaction();
    }

    /**
     * update （改） 一条数据
     */
    public void UpdateDog( String wlkh,String  courseId, String tiem) {
        PersonnelResutl dog = mRealm.where(PersonnelResutl.class).equalTo("wlkh", wlkh).equalTo("courseId",courseId).findFirst();
        mRealm.beginTransaction();
        dog.setTime(tiem);
        mRealm.commitTransaction();
    }

    /**
     * update （改） 一条数据
     */
    public void UpdateDogTye(String wlkh,String courseId, int Tye) {
        PersonnelResutl dog = mRealm.where(PersonnelResutl.class).equalTo("wlkh", wlkh).equalTo("courseId",courseId).findFirst();
        mRealm.beginTransaction();
        dog.setType(Tye);
        mRealm.commitTransaction();
    }
    /**
     * update （改） 一条数据
     */
    public void UpdateFindllTye( String courseId, int Tye) {
        PersonnelResutl dog = mRealm.where(PersonnelResutl.class).equalTo("courseId", courseId).findFirst();
        mRealm.beginTransaction();
        dog.setType(Tye);
        mRealm.commitTransaction();
    }

    /**
     * query （根据age查）
     */
    public  void   UpdateFindll(String courseId,String classId,String UserId,String time) {
        PersonnelResutl dogs = mRealm.where(PersonnelResutl.class).equalTo("courseId", courseId).equalTo("classId", classId).equalTo("UserId",UserId).findFirst();
        mRealm.beginTransaction();
        dogs.setTime(time);
        dogs.setType(1);
        mRealm.commitTransaction();
    }

    /**
     * query （根据age查）
     */
    public  void   UpdateFindlll(String classId,String UserId,String time) {
        List<PersonnelResutl>  dogs = mRealm.where(PersonnelResutl.class).equalTo("classId", classId).equalTo("UserId",UserId).findAll();
        mRealm.beginTransaction();
        for (int i=0;i<dogs.size();i++){
            dogs.get(i).setTime(time);
        }

        mRealm.commitTransaction();
    }
    /**
     * query （根据age查）
     */
    public  void   UpdateTime(String classId,String UserId,String time,String lg,String attendResult) {
        List<MeetingCheck> dogs = mRealm.where(MeetingCheck.class).equalTo("classId", classId).equalTo("UserId",UserId).findAll();
        mRealm.beginTransaction();
        for (int i=0;i<dogs.size();i++){
            dogs.get(i).setTime(time);
            dogs.get(i).setLg(lg);
            dogs.get(i).setAttendResult(attendResult);
        }
        mRealm.commitTransaction();
    }
    /**
     * query （根据age查）
     */
    public  void   UpdateTime(String classId,String UserId,String time) {
        List<MeetingCheck> dogs = mRealm.where(MeetingCheck.class).equalTo("classId", classId).equalTo("UserId",UserId).findAll();
        mRealm.beginTransaction();
        for (int i=0;i<dogs.size();i++){
            dogs.get(i).setTime(time);
        }
        mRealm.commitTransaction();
    }
    /**
     * query 根据（wlkh）和id查询
     */
    public List<Attachment> QueryImag(String courseId,String  classId) {
        RealmResults<Attachment> dogs = mRealm.where(Attachment.class).equalTo("courseId", courseId).equalTo("classId", classId).findAll();
        return mRealm.copyFromRealm(dogs);
    }
    /**
     * query （查询所有）
     */
    public List<MeetingCheck> queryAllDog() {
        RealmResults<MeetingCheck> dogs = mRealm.where(MeetingCheck.class).findAll();
        /**
         * 对查询结果，按Id进行排序，只能对查询结果进行排序
         */
        //增序排列
//        dogs=dogs.sort("bigenCard");
//        //降序排列
//        dogs=dogs.sort("id", Sort.DESCENDING);
        return mRealm.copyFromRealm(dogs);
    }

    public List<PersonnelResutl> queryAllPersonnlResult() {
        RealmResults<PersonnelResutl> dogs = mRealm.where(PersonnelResutl.class).findAll();
        /**
         * 对查询结果，按Id进行排序，只能对查询结果进行排序
         */
        //增序排列
//        dogs=dogs.sort("bigenCard");
//        //降序排列
//        dogs=dogs.sort("id", Sort.DESCENDING);
        return mRealm.copyFromRealm(dogs);
    }

    /**
     * //     * query （查询所有）
     * //
     */
    public List<MeetingResutl> QueryAll() {
        RealmResults<MeetingResutl> dogs = mRealm.where(MeetingResutl.class).findAll();
        /**
         * 对查询结果，按Id进行排序，只能对查询结果进行排序
         */
        //增序排列
//        dogs=dogs.sort("bigenCard");
//        //降序排列
//        dogs=dogs.sort("id", Sort.DESCENDING);
        return mRealm.copyFromRealm(dogs);
    }

    /**
     * //     * query （查询所有）
     * //
     */
    public List<PersonnelResutl> QueryAllPersonnelResutl() {
        RealmResults<PersonnelResutl> dogs = mRealm.where(PersonnelResutl.class).findAll();
        /**
         * 对查询结果，按Id进行排序，只能对查询结果进行排序
         */
        //增序排列
//        dogs=dogs.sort("bigenCard");
//        //降序排列
//        dogs=dogs.sort("id", Sort.DESCENDING);
        return mRealm.copyFromRealm(dogs);
    }

    /**
     * query （根据Id（主键）查）
     */
    public MeetingCheck queryDogById(String id) {
        MeetingCheck dog = mRealm.where(MeetingCheck.class).equalTo("wlkh", id).findFirst();
        return dog;
    }

    /**
     * query （根据Id（主键）查）
     */
    public List<MeetingCheck> queryDogList(String id,String UserId) {
        List<MeetingCheck> dog = mRealm.where(MeetingCheck.class).equalTo("wlkh", id).equalTo("UserId",UserId).findAll();
        return dog;
    }
    /**
     * query （根据Id（主键）查）
     */
    public List<MeetingCheck> queryDogMeeting(String classId,String UserId) {
        List<MeetingCheck> dog = mRealm.where(MeetingCheck.class).equalTo("courseId", classId).equalTo("UserId",UserId).findAll();
        return dog;
    }
    /**
     * query （根据Id（主键）查）
     */
    public List<MeetingCheck> queryDogList(String id,String courseId,String classId) {
        List<MeetingCheck> dog = mRealm.where(MeetingCheck.class).equalTo("wlkh", id).equalTo("courseId", courseId).equalTo("classId", classId).findAll();
        return dog;
    }
    /**
     * query （根据Id（主键）查）
     */
    public List<MeetingCheck> queryDogListwlkh(String id,String classId) {
        List<MeetingCheck> dog = mRealm.where(MeetingCheck.class).equalTo("wlkh", id).equalTo("classId", classId).findAll();
        return dog;
    }
    /**
     * query 最后一条数据
     */
    public MeetingCheck queryDogByIdLast(String id) {
        RealmResults<MeetingCheck> realmResults = mRealm.where(MeetingCheck.class).equalTo("wlkh", id).findAll();
        MeetingCheck dog = realmResults.get(realmResults.size() - 1);
        return dog;
    }

    /**
     * query 第一条数据
     */
    public MeetingCheck queryDogByIdOne(String id) {
        RealmResults<MeetingCheck> realmResults = mRealm.where(MeetingCheck.class).equalTo("wlkh", id).findAll();
        MeetingCheck dog = realmResults.get(0);
        return dog;
    }

    /**
     * query （根据age查）
     */
    public List<PersonnelResutl> queryDobByAge(String courseId,String classId) {
        RealmResults<PersonnelResutl> dogs = mRealm.where(PersonnelResutl.class).equalTo("courseId", courseId).equalTo("classId", classId).findAll();
        dogs = dogs.sort("time", Sort.DESCENDING);
        return mRealm.copyFromRealm(dogs);
    }



    public  List<PersonnelResutl> querPersonne(String classId) {
        RealmResults<PersonnelResutl> dogs = mRealm.where(PersonnelResutl.class).equalTo("courseId", classId).findAll();
        return mRealm.copyFromRealm(dogs);

    }
    public  List<MeetingCheck> querMeetingCheck(String courseId,String classId) {
        RealmResults<MeetingCheck> dogs = mRealm.where(MeetingCheck.class).equalTo("courseId", courseId).equalTo("classId", classId).findAll();
        return mRealm.copyFromRealm(dogs);

    }
    public  List<MeetingCheck> querMeeting(String classId) {
        RealmResults<MeetingCheck> dogs = mRealm.where(MeetingCheck.class).equalTo("classId", classId).findAll();
        return mRealm.copyFromRealm(dogs);

    }
    public  List<MeetingCheck> querMeeting(String classId,String UserId) {
        RealmResults<MeetingCheck> dogs = mRealm.where(MeetingCheck.class).equalTo("classId", classId).equalTo("UserId",UserId).findAll();
        return mRealm.copyFromRealm(dogs);

    }

    public  List<MeetingResutl> querMeetingResutll(String classId) {
        RealmResults<MeetingResutl> dogs = mRealm.where(MeetingResutl.class).equalTo("classId", classId).findAll();
        return mRealm.copyFromRealm(dogs);
    }
    public  List<MeetingResutl> querMeetingResutll(String courseId,String classId) {
        RealmResults<MeetingResutl> dogs = mRealm.where(MeetingResutl.class).equalTo("courseId", courseId).equalTo("classId", classId).findAll();
        return mRealm.copyFromRealm(dogs);
    }


    public  List<MeetingResutl> querMeetingResutll(String classId,String beginTime,String endTime) {
        RealmResults<MeetingResutl> dogs = mRealm.where(MeetingResutl.class).equalTo("classId", classId).equalTo("beginTime",beginTime).equalTo("endTime",endTime)
                .findAll();
        return mRealm.copyFromRealm(dogs);

    }
    public  List<MeetingResutl> querMeetingResutl(String classId) {
        RealmResults<MeetingResutl> dogs = mRealm.where(MeetingResutl.class).equalTo("courseId", classId).findAll();
        return mRealm.copyFromRealm(dogs);

    }
    public  List<MeetingResutl> querMeetingResutl(String classId,String beginTime,String endTime) {
        RealmResults<MeetingResutl> dogs = mRealm.where(MeetingResutl.class).equalTo("courseId", classId).equalTo("beginTime",beginTime).equalTo("endTime",endTime).
                findAll();
        return mRealm.copyFromRealm(dogs);

    }
    /**
     * query 根据（wlkh）和id查询
     */
    public List<MeetingCheck> QueryDobByAge(String wlkh, String courseId) {
        RealmResults<MeetingCheck> dogs = mRealm.where(MeetingCheck.class).equalTo("wlkh", wlkh).equalTo("courseId", courseId).findAll();
        return mRealm.copyFromRealm(dogs);
    }
    public List<MeetingCheck> QueryTraineeId(String UserId, String courseId) {
        RealmResults<MeetingCheck> dogs = mRealm.where(MeetingCheck.class).equalTo("UserId", UserId).equalTo("courseId", courseId).findAll();
        return mRealm.copyFromRealm(dogs);
    }
    public List<MeetingCheck> QueryTraineeClassd(String UserId, String classId) {
        RealmResults<MeetingCheck> dogs = mRealm.where(MeetingCheck.class).equalTo("UserId", UserId).equalTo("classId", classId).findAll();
        return mRealm.copyFromRealm(dogs);
    }
    /**
     *  获取第一个MeetingCheck
     * @param UserId
     * @param courseId
     * @return
     */
    public MeetingCheck QueryTraineeIdOne(String UserId, String courseId, int type) {
        RealmResults<MeetingCheck> dogs=null;
        MeetingCheck dog;
        dogs = mRealm.where(MeetingCheck.class).equalTo("UserId", UserId).equalTo("courseId", courseId).findAll();
        if (type==1){
            dog=   dogs.get(0);
        }else {
            dog= dogs.get(dogs.size()-1);
        }

        return dog;
    }
    /**
     *  获取第一个MeetingCheck
     * @param UserId
     * @param classId
     * @return
     */
    public MeetingCheck QueryTraineeId(String UserId, String classId, int type) {
        RealmResults<MeetingCheck> dogs=null;
        MeetingCheck dog;
        dogs = mRealm.where(MeetingCheck.class).equalTo("UserId", UserId).equalTo("classId", classId).findAll();
        if (type==1){
            dog=   dogs.get(0);
        }else {
            dog= dogs.get(dogs.size()-1);
        }

        return dog;
    }
//    public List<MeetingCheck> QueryTraineeId(String UserId, String courseId) {
//        RealmResults<MeetingCheck> dogs = mRealm.where(MeetingCheck.class).equalTo("UserId", UserId).equalTo("courseId", courseId).findAll();
//        return mRealm.copyFromRealm(dogs);
//    }

    public boolean isDogExist(String id,String courseId,String classId) {
        PersonnelResutl dog = mRealm.where(PersonnelResutl.class).equalTo("wlkh", id).equalTo("courseId", courseId).equalTo("classId", classId).equalTo("useState",1).findFirst();
        if (dog == null) {
            return false;
        } else {
            return true;
        }
    }

    public Realm getRealm() {

        return mRealm;
    }

    public void close() {
        if (mRealm != null) {
            mRealm.close();
        }
    }

    /**
     * add （增）
     */
    public void addTempBean(final TempBean dog) {
        mRealm.beginTransaction();
        mRealm.copyToRealm(dog);
        mRealm.commitTransaction();

    }
    /**
     * query （查询所有）
     */
    public List<TempBean> queryAllTempBean() {
        RealmResults<TempBean> dogs = mRealm.where(TempBean.class).findAll();
        /**
         * 对查询结果，按Id进行排序，只能对查询结果进行排序
         */
        //增序排列
        dogs = dogs.sort("time", Sort.ASCENDING);
        return mRealm.copyFromRealm(dogs);
    }


}
